import { rdsRequest, rdsRequestAsync, getRdsUrl } from './rdsServices';


let c_adno_level = 1;
let adnoLevel = 1;
const getBillInfo = async ({ billKind: billKind, tenantId: tenantId }) => {
    const rdsUrl = getRdsUrl();
    if (!billKind || !tenantId) {
        return null;
    }
    const req = {
        WhereList: [
            { c_col: "c_tenant_id", c_oper: "等于", c_value: tenantId, c_logic: "而且" },
            { c_col: "c_bill_kind", c_oper: "等于", c_value: billKind, c_logic: "而且" },
        ]
    }
    const res = await rdsRequest(rdsUrl.mainApi, 'billtype', 'EnjoyQueryList', req);
    const result = res.ReturnObject.find(item => item.c_bill_kind === billKind);
    c_adno_level = result?.c_adno_level;
    return result;
};

/**
 * 获取部门层级
 * @param {部门，部门层级} param0 
 * @returns 
 */
const getAdnoLevel = async ({ IuputData: IuputData, adnoLevel: adnoLevel }) => {
    const rdsUrl = getRdsUrl();
    if (!IuputData || !adnoLevel) {
        return null;
    }
    const req = {
        IuputData: IuputData,
        adnoLevel: adnoLevel
    }
    const res = await rdsRequestAsync(rdsUrl.bcApi, '获取部门名称', '', req);
    return res;
}


/**
 * 验证部门层级
 * @param {部门} param0 
 * @returns 
 */
const validateAdnoLevel = async ({ IuputData: IuputData, billKind: billKind, tenantId: tenantId }) => {
    const rdsUrl = getRdsUrl();
    if (!IuputData || !billKind || !tenantId) {
        return null;
    }
    await getBillInfo({ billKind: billKind, tenantId: tenantId });
    const req = {
        IuputData: IuputData,
        adnoLevel: c_adno_level
    }
    const res = await rdsRequestAsync(rdsUrl.bcApi, '获取部门名称', '', req);
    return res;
}

export {
    adnoLevel,
    getBillInfo,
    getAdnoLevel,
    validateAdnoLevel
};